package com.hello.pet.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * swagger文档
 */
@EnableSwagger2
@Configuration
public class SwaggerConfig {

    //构造api文档的信息
    private ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                .title("平城宠物小程序----API文档")
                .contact(new Contact("hello","https://gitee.com/gold_city/pet","484155842@qq.com"))
                .version("开发版本")
                .build();
    }

    //构建spring bean
    @Bean
    public Docket createRestApi(){
        //配置Spring Security登录认证
        ParameterBuilder parameterBuilder = new ParameterBuilder();
        List<Parameter> parameters = new ArrayList<>();
        parameterBuilder.name("Authorization").description("token").modelRef(new ModelRef("String"))
                .parameterType("Header").required(false).build();
        parameters.add(parameterBuilder.build());
        //构建
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.hello.pet"))
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(parameters);
    }
}
